const app = getApp();

Component({
  properties: {
    //名称
    name: {
      type: null,
      value: '附件'
    },
    //图片数组
    image: {
      type: Array,
      value: [],
    },
    //最大长度
    maxlength: {
      type: Number,
      value: 9
    },
    //是否剪切
    cut: {
      type: String,
      value: 0
    },
    //标识符号
    index: {
      type: String,
      value: ''
    },
    mode: {
      type: String,
      value: ''
    }
  },
  data: {
    showMore: false
  },
  lifetimes: {
    attached: function() {
      this.properties.image = []
    },
  },
  methods: {
    chooseimage: function(e) {

      var that = this;
      var index = e.currentTarget.dataset.index;
      wx.chooseImage({
        count: that.properties.cut == 1 ? 1 : that.properties.maxlength - that.properties.image.length,
        success: function(res) {
          var sessionID = wx.getStorageSync('USER_SESSIONID');
          var objectDefineID = app.objectdefineID;
          var memberID = wx.getStorageSync('USER_MEMBERID');
          var tempFilePaths = res.tempFilePaths;
          var serverUrl = app.serverUrl;
          var length = tempFilePaths.length;
          var maxlength = that.properties.maxlength ? that.properties.maxlength : 9;
          //隐藏新增图片，限制上传
          if (length + that.properties.image.length >= maxlength) {
            that.setData({
              hideMore: true
            })
          } else that.setData({
            hideMore: false
          })
          //*** */
          if (!that.properties.cut) {
            wx.showLoading({
              title: '上传中',
            })
            var image = that.properties.image;
            var sum = 0;
            for (var i in tempFilePaths) {
              wx.uploadFile({
                url: app.serverUrl + '/files/qiniu.json',
                filePath: tempFilePaths[i],
                name: 'file',
                dataType: 'json',
                success(res) {
                  var data = JSON.parse(res.data);
                  var url = data.body.url;
                  image.push({
                    url
                  });
                  sum++;
                  if (sum == tempFilePaths.length) {
                    wx.hideLoading()
                    that.triggerEvent("changeImg", {
                      image,
                      index: that.properties.index
                    })
                  }
                }
              })
            }

          } else {
            const src = res.tempFilePaths[0]
            var images = that.properties.image;
            wx.navigateTo({
              url: '/pages/public/upload/upload?src=' + src + "&name=" + that.properties.name + '&images=' + JSON.stringify(images)
            })
          }
        }
      })
    },

    delImg(e) {
      var image = this.properties.image;
      var index = e.currentTarget.dataset.index;
      var currentIndex = this.properties.index;;
      var delImg = {};
      delImg = image[index];
      var that = this;
      image.splice(index, 1);
      this.triggerEvent('delImg', {
        image: image,
        index: index,
        delImg: delImg,
        currentIndex
      })
      this.setData({
        hideMore: false
      })
    }
  },

});

function removeObjectPicture(that, attid) {
  api.request(app, 'removeObjectPicture.json', {
    sessionID: wx.getStorageSync('USER_SESSIONID'),
    attachmentID: attid
  }, (res) => {
  })
}